<?php

require("includes/config.php"); 
require("includes/function.common.php");

##################################################################################
# GLOBAL Variables

$reply;
$player;
$desk;
$round;

#
##################################################################################

##################################################################################
# Functions

function initialize()
{
	global $debug, $dsn, $dbUser, $dbPW, $database, $query, $sth;
	$cardposition;
	
	try
	{
		$database = new PDO($dsn, $dbUser, $dbPW);
		$database->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING );
	}
	catch (PDOException $e)
	{
		echo 'Connection failed: ' . $e->getMessage();
	}

	##################################################################################
	# Shuffle the Cards
	
	$query = "UPDATE cards SET place = 2";
	$sth = $database->prepare($query);
	
	try 
	{
		if ( !($result = $sth->execute()) )
		{
			echo "No result has beeb updated";
		}
	}
	catch (PDOException $e)
	{
		echo "Execute failed" . $e->getMessage();
	}
	
	$cardposition = range(1, 81);
	shuffle($cardposition);
	for ($i = 0; $i < 81; $i++)
	{
		echo "($i, $cardposition[$i]) ";
	}
	
	$query = "UPDATE cards SET sequence = ? WHERE cid = ?";
	$sth = $database->prepare($query);
	
	for ($i = 1; $i < 82; $i++)
	{
		$sth->bindParam(1, $cardposition[$i - 1]);
		$sth->bindParam(2, $i);
		
		try
		{
			$result = $sth->execute();
		}
		catch (PDOException $e)
		{
			echo 'Shuffle card for init failed, Exception: ' .  $e->getMessage();
		}
if ($debug == 'yes') echo "($result $i)";
	}
	
	$query = "UPDATE states SET phase = 0, state = 0, turn = 0, player = 0";
	$sth = $database->prepare($query);
	
	try
	{
		$result = $sth->execute();
	}
	catch (PDOException $e)
	{
		echo 'State initialize failed, Exception: ' .  $e->getMessage();
	}
	
	changeState(0,0,0,0);
}

// Autoselect by server, not a user action
function selectIdentity()
{
	global $debug, $dsn, $dbUser, $dbPW, $database, $query, $sth;
	$player_no;
	$id_list;
	$red;
	$blue;
	$green;
	
	if ($database == null)
	{
		try
		{
			$database = new PDO($dsn, $dbUser, $dbPW);
			$database->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING );
		}
		catch (PDOException $e)
		{
			echo 'Connection failed: error ' . $e->getMessage();
		}
	}
	
	// Retrieve the identity mapping
	
	$query = "SELECT red, blue, green FROM id_map m 
	          WHERE m.player_num = (SELECT COUNT(*) FROM players)";
	$sth = $database->prepare($query);
	
	try
	{
		$sth->execute();
		if ($result = $sth->fetch())
		{
			$red = (int) $result[0];
			$blue = (int) $result[1];
			$green = (int) $result[2];
		}
	}
	catch (PDOException $e)
	{
		echo 'Execute error: ' . $e->getMessage();
	}
	
	$id_list = Array();

	for ($i = 0; $i < $red; $i++)
	{
		array_push($id_list, 1);
	}

	for ($i = 0; $i < $blue; $i++)
	{
		array_push($id_list, 2);
	}

	for ($i = 0; $i < $green; $i++)
	{
		array_push($id_list, 3);
	}
	
	print_r($id_list);

	$length = count($id_list);
	shuffle($id_list);

	$query = "UPDATE players SET identity = ? WHERE player = ?";
	$sth = $database->prepare($query);
	
	try
	{
		for ($i = 1; $i <= $length; $i++)
		{
			$sth->BindParam(1, $id_list[$i - 1]);
			$sth->BindParam(2, $i);
			
if ($debug == 'yes') echo $id_list[$i - 1] .",". $i . "<br />";
			
			if ( !($result = $sth->execute()) )
			{
				echo 'Update identity failed.';
			}
		}
	}
	catch (PDOException $e)
	{
		echo 'Identity query Exception: ' . $e->getMessage();
	}
}




?>